package com.zyz.O_网络编程.UDP.demo2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;

/**
 * @author: vick
 * @Date: 2025/4/17
 * @Description:
 * @version: 1.0
 */
public class Send {
    public static void main(String[] args) throws Exception {
        //1. 建立发送端的DatagramSocket
        DatagramSocket datagramSocket = new DatagramSocket();

        //要发送的数据
        ArrayList<String> messages = new ArrayList<>();
        messages.add("hello");
        messages.add("Java");
        messages.add("Python");
        messages.add("JS");
        messages.add("Go");
        messages.add("Rust");

        InetAddress ip = InetAddress.getByName("127.0.0.1");
        int port = 8888;
        //发送多个数据
        for (int i = 0; i < messages.size(); i++) {
            //2. 建立数据包
            byte[] data = messages.get(i).getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(data, 0, data.length, ip, port);
            datagramSocket.send(datagramPacket);
        }

        //4.关闭
        datagramSocket.close();
    }
}
